<template>
  <div class="login-bg">
    <div class="particle-layer">
      <div v-for="n in 40" :key="n" class="particle"></div>
    </div>
    <div class="login-container">
      <div class="login-right">
        <el-card class="login-card">
          <div class="login-title">蜀湘曹氏鸭脖</div>
          <el-form :model="form" :rules="rules" ref="formRef" label-width="80px" @keyup.enter="onLogin">
            <el-form-item label="账号" prop="account">
              <el-input v-model="form.account" placeholder="请输入账号" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="form.password" type="password" placeholder="请输入密码" show-password />
            </el-form-item>
            <el-form-item>
              <el-checkbox v-model="form.remember">下次自动登录</el-checkbox>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" :loading="loading" @click="onLogin" style="width:100%">登录</el-button>
            </el-form-item>
          </el-form>
        </el-card>
        <div class="login-footer">
          <span>©1998 - 2025 曹氏鸭脖. 保留所有权利.</span>
          <span>粤ICP备xxxxxxx号</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { useUserStore } from '../store/user';
import axios from 'axios';

const router = useRouter();
const userStore = useUserStore();
const formRef = ref();
const loading = ref(false);

const form = ref({
  account: '',
  password: '',
  remember: false,
});

const rules = {
  account: [
    { required: true, message: '请输入账号', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少为6位', trigger: 'blur' }
  ]
};

onMounted(() => {
  // 自动登录
  if (userStore.autoLogin() && userStore.token) {
    router.replace({ path: '/' });
  }
});

const onLogin = async () => {
  try {
    await formRef.value.validate();
    loading.value = true;
    const { data } = await axios.post('http://121.43.176.9:8090/api/user/login', {
      account: form.value.account,
      password: form.value.password
    });
    if (data.code === 200) {
      userStore.login({
        token: data.data,
        account: form.value.account,
        remember: form.value.remember,
      });
      ElMessage.success('登录成功');
      router.replace({ path: '/home' });
    } else {
      ElMessage.error(data.msg || '登录失败');
    }
  } catch (err) {
    // 表单校验失败或网络错误
    if (err && err.message) {
      ElMessage.error(err.message);
    }
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped>
.login-bg {
  position: fixed;
  inset: 0;
  z-index: 0;
  overflow: hidden;
  transition: background 0.5s;
}
.login-bg::before {
  content: '';
  position: absolute;
  inset: 0;
  z-index: 1;
  opacity: 0.92;
  animation: gradientMove 8s ease-in-out infinite alternate;
}
@keyframes gradientMove {
  0% {
    background-position: 0% 50%;
  }
  100% {
    background-position: 100% 50%;
  }
}
.particle-layer {
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
}
.particle {
  position: absolute;
  border-radius: 50%;
  background: rgba(229,57,53,0.18);
  width: 10px;
  height: 10px;
  opacity: 0.7;
  animation: particleMove 12s linear infinite;
}
.particle:nth-child(3n) {
  background: rgba(229,57,53,0.32);
  width: 14px;
  height: 14px;
  animation-duration: 16s;
}
.particle:nth-child(4n) {
  background: rgba(255,255,255,0.25);
  width: 8px;
  height: 8px;
  animation-duration: 10s;
}
.particle:nth-child(5n) {
  background: rgba(229,57,53,0.12);
  width: 18px;
  height: 18px;
  animation-duration: 20s;
}
.particle {
  left: calc(100% * var(--rand-x, 0.1));
  top: calc(100% * var(--rand-y, 0.1));
  animation-delay: calc(-1s * var(--rand-delay, 0));
}
.particle-layer .particle {
  --rand-x: calc((var(--i, 1) * 13) % 100 / 100);
  --rand-y: calc((var(--i, 1) * 29) % 100 / 100);
  --rand-delay: calc(var(--i, 1) * 2 % 10);
}
.particle-layer .particle:nth-child(n) { --i: 1; }
.particle-layer .particle:nth-child(2) { --i: 2; }
.particle-layer .particle:nth-child(3) { --i: 3; }
.particle-layer .particle:nth-child(4) { --i: 4; }
.particle-layer .particle:nth-child(5) { --i: 5; }
.particle-layer .particle:nth-child(6) { --i: 6; }
.particle-layer .particle:nth-child(7) { --i: 7; }
.particle-layer .particle:nth-child(8) { --i: 8; }
.particle-layer .particle:nth-child(9) { --i: 9; }
.particle-layer .particle:nth-child(10) { --i: 10; }
.particle-layer .particle:nth-child(11) { --i: 11; }
.particle-layer .particle:nth-child(12) { --i: 12; }
.particle-layer .particle:nth-child(13) { --i: 13; }
.particle-layer .particle:nth-child(14) { --i: 14; }
.particle-layer .particle:nth-child(15) { --i: 15; }
.particle-layer .particle:nth-child(16) { --i: 16; }
.particle-layer .particle:nth-child(17) { --i: 17; }
.particle-layer .particle:nth-child(18) { --i: 18; }
.particle-layer .particle:nth-child(19) { --i: 19; }
.particle-layer .particle:nth-child(20) { --i: 20; }
.particle-layer .particle:nth-child(21) { --i: 21; }
.particle-layer .particle:nth-child(22) { --i: 22; }
.particle-layer .particle:nth-child(23) { --i: 23; }
.particle-layer .particle:nth-child(24) { --i: 24; }
.particle-layer .particle:nth-child(25) { --i: 25; }
.particle-layer .particle:nth-child(26) { --i: 26; }
.particle-layer .particle:nth-child(27) { --i: 27; }
.particle-layer .particle:nth-child(28) { --i: 28; }
.particle-layer .particle:nth-child(29) { --i: 29; }
.particle-layer .particle:nth-child(30) { --i: 30; }
.particle-layer .particle:nth-child(31) { --i: 31; }
.particle-layer .particle:nth-child(32) { --i: 32; }
.particle-layer .particle:nth-child(33) { --i: 33; }
.particle-layer .particle:nth-child(34) { --i: 34; }
.particle-layer .particle:nth-child(35) { --i: 35; }
.particle-layer .particle:nth-child(36) { --i: 36; }
.particle-layer .particle:nth-child(37) { --i: 37; }
.particle-layer .particle:nth-child(38) { --i: 38; }
.particle-layer .particle:nth-child(39) { --i: 39; }
.particle-layer .particle:nth-child(40) { --i: 40; }
@keyframes particleMove {
  0% {
    transform: translateY(0) scale(1);
    opacity: 0.7;
  }
  80% {
    opacity: 0.7;
  }
  100% {
    transform: translateY(-80vh) scale(1.2);
    opacity: 0;
  }
}
.login-container {
  position: relative;
  z-index: 10;
  display: flex;
  min-height: 100vh;
  background: none;
  justify-content: center;
  align-items: center;
}
.login-card {
  width: 360px;
  margin-bottom: 32px;
  box-shadow: 0 2px 16px rgba(229,57,53,0.08);
}
.login-title {
  font-size: 22px;
  font-weight: 600;
  color: #e53935;
  text-align: center;
  margin-bottom: 24px;
}
.login-footer {
  text-align: center;
  color: #000000;
  font-size: 13px;
  margin-top: 16px;
  line-height: 1.8;
}
.login-actions {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 8px;
  margin-bottom: -8px;
}
.action-btn {
  color: #e53935;
  font-size: 15px;
  padding: 0 8px;
}
.divider {
  color: #bbb;
  margin: 0 4px;
  font-size: 15px;
}
</style> 